package com.tannm.doan.taskmanager.database;

import java.util.Calendar;
import android.content.*;

public class DataField {
	// types
	public static enum Type {
		INT, TEXT, BOOL
	};

	// fields
	private Calendar dateOut = Calendar.getInstance();

	// fields
	private DataRow dataRow = null;
	private ContentValues values = null;

	// fields
	private int index = 0;
	private String sName = "";
	private Type fieldType = Type.INT;
	private boolean bCanBeNull = true;
	private boolean bPrimaryKey = false;

	// methods
	public DataField(int index, String sName, Type fieldType,
			boolean bCanBeNull, boolean bPrimaryKey) {
		this.index = index;
		this.sName = sName;
		this.fieldType = fieldType;
		this.bCanBeNull = bCanBeNull;
		this.bPrimaryKey = bPrimaryKey;
	}

	public String getColumnDefinition() {
		String s = sName + " " + getSqlType(fieldType);
		if (bPrimaryKey)
			s += " PRIMARY KEY";
		if (!bCanBeNull)
			s += " NOT NULL";
		return s;
	}

	public Type getType() {
		return fieldType;
	}

	public int getIndex() {
		return index;
	}

	public String getSqlType(Type value) {
		switch (value) {
		case INT:
			return "INTEGER";
		case TEXT:
			return "TEXT";
		case BOOL:
			return "INTEGER";
		}
		return "TEXT";
	}

	public void setParentRow(DataRow dataRow) {
		this.dataRow = dataRow;
		this.values = this.dataRow.getContentValues();
	}

	public String getName() {
		return sName;
	}

	// getters
	public String asString() {
		return values.getAsString(sName);
	}

	public long asLong() {
		return values.getAsLong(sName);
	}

	public boolean asBoolean() {
		return (values.getAsLong(sName) == 1);
	}

	public boolean isNull() {
		return (values.get(sName) == null);
	}

	public Calendar asCalendar() {
		dateOut.setTimeInMillis(values.getAsLong(sName));
		return dateOut;
	}

	// setters
	public void set(String value) {
		values.put(sName, value);
	}

	public void set(long value) {
		values.put(sName, value);
	}

	public void set(boolean value) {
		int i = (value) ? 1 : 0;
		values.put(sName, i);
	}

	public void set(Calendar value) {
		values.put(sName, value.getTimeInMillis());
	}

	public void setNull() {
		values.put(sName, (String) null);
	}
}
